<template>
  <div class="profile-container">
    <!-- 未登录显示的盒子 -->
    <div class="not-login bg" v-if="!user">
       <img src="@/assets/mobile.png" alt="" class="pic" @click="$router.push('/login')">
       <span @click="$router.push('/login')">登录 / 注册</span>
    </div>

    <!-- 登录显示的盒子 -->
    <div class="login-box bg" v-else>
      <div class="user-info">
        <div class="left">
          <van-image
            round
            fit="cover"
            :src='userInfo.photo'
          />
          <span>{{userInfo.name}}</span>
        </div>
        <van-button type="default" size="mini">编辑资料</van-button>
      </div>
      <div class="user-data">
        <div class="data-item">
          <span>{{userInfo.art_count}}</span>
          <span>头条</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.follow_count}}</span>
          <span>关注</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.fans_count}}</span>
          <span>粉丝</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.like_count}}</span>
          <span>获赞</span>
        </div>
      </div>
    </div>

   <!-- 宫格导航 -->
    <van-grid clickable :column-num="2">
      <van-grid-item icon="home-o" text="收藏" to="/shoucang" >
        <template #icon>
          <i class="toutiao toutiao-shoucang"></i>
        </template>
      </van-grid-item>
      <van-grid-item icon="search" text="历史" to="/lishi" >
        <template #icon>
          <i class="toutiao toutiao-lishi"></i>
        </template>
      </van-grid-item>
    </van-grid>

    <div class="marginBox">
      <van-cell title="单元格" is-link />
      <van-cell title="单元格" is-link />
    </div>

    <!-- 退出登录按钮    -->
    <van-cell title="退出登录" @click="escLoginFn" class="logout" v-if="user"/>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { HMTT_TOKEN } from '@/store'
import { Dialog, Toast } from 'vant'
import { getUserInfoAPI } from '@/api/user.js'
export default {
  computed: {
    ...mapState(['user'])
  },
  created () {
    if (this.user) {
      this.getUserInfoFn()
    }
  },
  data: function () {
    return {
      userInfo: {} // 保存获取到的用户信息 渲染到页面上
    }
  },

  methods: {
    escLoginFn () {
      Dialog.confirm({
        title: '黑马头条',
        message: '确认退出登录?'
      })
        .then(() => {
          // 取消登录清空token 和vuex中的数据user
          this.$store.commit('clearUser', HMTT_TOKEN)
        })
        .catch(() => {
          // on cancel
        })
    },
    // 获取用户信息的方法
    async getUserInfoFn () {
      try {
        const res = await getUserInfoAPI()
        this.userInfo = res
      } catch (error) {
        Toast('系统异常')
      }
    }
  }
}
</script>

<style scoped lang='less'>
.profile-container {
  // padding-bottom: 200px;
  .bg {
     width: 100%;
    height: 401px;
    background-image: url(~@/assets/banner.png);
    background-size: 100%;
  }
  .not-login {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
   .pic {
    width: 132px;
    height: 132px;
   }
   span {
     font-size: 28px;
     color: #fff;
     margin-top: 15px;
   }
  }
  .login-box {
    overflow: hidden;
    .user-info {
      height: 132px;
      margin-top: 116px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding:0 32px;
      .left {
        display: flex;
        align-items: center;
        .van-image {
          width: 132px;
          height: 132px;
        }
        span {
          color: #fff;
          font-size: 29px;
          margin-left: 22px;
        }
      }
      .van-button {
        width: 115px;
        height: 32px;
        font-size: 20px;
        color: #666;
        padding: 0;
        border-radius: 16px;
      }
    }
    .user-data{
      height: 153px;
      padding:0 32px;
      display: flex;
      .data-item {
         flex:1;
         display: flex;
        //  修改主轴方向 为y轴
         flex-direction: column;
         // 侧轴居中
         align-items: center;
         // 主轴居中
         justify-content: center;
         color: #fff;
         span:nth-child(1){
           font-size: 26px;
         };
         span:nth-child(2){
           font-size: 23px;
           margin-top: 12px;
         }
      }
    }
  }
  .van-grid{
    .toutiao {
      font-size: 45px;
    }
    .toutiao-shoucang {
      color: #EB5253;
    }
    .toutiao-lishi {
      color: #FF9D1D;
    }
    /deep/.van-grid-item__text {
      font-size: 27px;
      color: #333;
      margin-top:  10px;
      letter-spacing: 20px;
      padding-left: 20px;
    }
  }
  .marginBox {
    margin: 10px 0;
  }
  .logout {
    text-align: center;
    color: #D86262;
    font-size: 29px;
  }
}
</style>
